package com.kitty.game.task.facade;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.activity.service.task.TaskActivityHandler;
import com.kitty.game.activity.service.task.XuanShangTaskHandler;
import com.kitty.game.config.TaskSet;
import com.kitty.game.market.MarketService;
import com.kitty.game.npc.message.RespAutoWalk;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.task.message.ReqAutoTaskState;
import com.kitty.game.task.message.ReqStartXsAutoWalk;
import com.kitty.game.task.message.vo.TaskInfo;
import com.kitty.game.task.service.CangBaoTuService;
import com.kitty.game.task.service.NewTaskService;
import com.kitty.game.task.service.message.AllAutoCangBaoTu;
import com.kitty.game.task.service.message.ReqCloseAutoCangBaoTu;
import com.kitty.game.task.service.message.ReqOpenAutoCangBaoTu;
import com.kitty.game.task.service.taskHandler.*;
import com.kitty.listener.EventType;
import com.kitty.listener.annotation.EventHandler;
import com.kitty.listener.event.HeartBeatEvent;
import com.kitty.listener.event.LoginEvent;
import com.kitty.listener.event.RemovePetEvent;
import com.kitty.listener.event.RoleLevelUpEvent;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
@Slf4j
public class TaskController {
    @Autowired
    CangBaoTuService cangBaoTuService;
    /**
     * 在心跳事件中处理超时任务
     */
    @EventHandler(EventType.HEART_BEAT)
    public void handleHeartBeat(HeartBeatEvent heartBeatEvent) {
        Role role = heartBeatEvent.getRole();
        if (role == null) {
            return;
        }
        TaskHelper.INSTANCE.getHandlers().values().forEach(taskHandler -> {
            if (taskHandler instanceof TimeTaskHandler) {
                ((TimeTaskHandler) taskHandler).handleHeartBeat(role, heartBeatEvent.getPrevHeartTime());
            }
        });
    }


    /**
     * 为了避免上线的时候任务的显示时间负数 在登录事件里面也处理一下
     */
    @EventHandler(EventType.LOGIN)
    public void handleLoginEvent(LoginEvent loginEvent) {
        Role role = loginEvent.getRole();
        if (role == null) {
            return;
        }
        TaskHelper.INSTANCE.getHandlers().values().forEach(taskHandler -> taskHandler.handleLogin(role));
    }

    /**
     * 不知道有什么用 写在这里只是不让提示而已
     */
    @RequestMapping
    public void autoTaskState(IoSession session, ReqAutoTaskState autoTaskState) {
        if(autoTaskState.getActionType() == 50 && autoTaskState.getStage() == 1){
            Role role = SessionUtils.getRoleBySession(session);
            TaskSet taskSet = SpringUtils.getBean(DigTreasureTaskHandler.class).getTaskSetByType(role);

            if (SpringUtils.getBean(DigTreasureTaskHandler.class).checkStartDig(role, taskSet)) {
                /**挖宝时，则不触发其他任务*/

            }
//            String taskName = taskSet.getTaskName();
//            log.info("藏宝图任务名称："+taskName);
//            if("特级宝藏".equals(taskName)){
//                ReqOpenAutoCangBaoTu reqOpenAutoCangBaoTu = new ReqOpenAutoCangBaoTu();
//                reqOpenAutoCangBaoTu.setItemName("特级藏宝图");
//                cangBaoTuService.autoCangBaoTu(role,reqOpenAutoCangBaoTu);
//            }

        }

    }
    /**
     * 自动挖宝图
     */
    @RequestMapping
    public void autoCangBaoTu(IoSession session, AllAutoCangBaoTu allAutoCangBaoTu){
        /**需要先验证账号权限*/
        Role role = SessionUtils.getRoleBySession(session);
        cangBaoTuService.autoCangBaoTu(role,allAutoCangBaoTu);
    }
    /**
     * 自动挖宝图
     */
    @RequestMapping
    public void autoCangBaoTu2(IoSession session, ReqOpenAutoCangBaoTu reqOpenAutoCangBaoTu){
        /**需要先验证账号权限*/
        Role role = SessionUtils.getRoleBySession(session);
        cangBaoTuService.autoCangBaoTu(role,reqOpenAutoCangBaoTu);
    }
    //关闭自动挖宝图
    @RequestMapping
    public void closeAutoCangBaoTu(IoSession session, ReqCloseAutoCangBaoTu reqOpenAutoCangBaoTu){
        /**需要先验证账号权限*/
        Role role = SessionUtils.getRoleBySession(session);
        cangBaoTuService.stopAuto(role,reqOpenAutoCangBaoTu);
    }
    /**
     * 悬赏自动寻路
     */
    @RequestMapping
    public void startXsAutoWalk(IoSession session, ReqStartXsAutoWalk reqStartXsAutoWalk) {
        NewTaskService taskService = SpringUtils.getTaskService();
        Role role = SessionUtils.getRoleBySession(session);
        TaskSet fightTask = taskService.getTaskSet(112, role);
        TaskSet giftTask = taskService.getTaskSet(113, role);

        TaskInfo taskInfo = null;
        if (taskService.isTaskAccepted(role, fightTask)) {
            taskInfo = SpringUtils.getBean(XuanShangTaskHandler.class).getTaskInfo(role, fightTask);
        } else if (taskService.isTaskAccepted(role, giftTask)) {
            taskInfo = SpringUtils.getBean(XuanShangTaskHandler.class).getTaskInfo(role, giftTask);
        }

        if (taskInfo != null) {
            MessagePusher.pushMessage(session, new RespAutoWalk(taskInfo.getTaskZhiyin()));
        }
    }

    @EventHandler(EventType.REMOVE_PET)
    public void handleRemovePetEvent(RemovePetEvent removePetEvent) {
        Pet pet = removePetEvent.getPet();
        Role role = SpringUtils.getRoleService().getOnlinePlayer(pet.getHostId());
        SpringUtils.getBean(PetFlyTaskHandler.class).checkWhileRemovePet(role, pet);
    }

    /**
     * 处理角色升级事件
     */
    @EventHandler(EventType.ROLE_LEVEL_UP)
    public void handleRoleLevelUpEvent(RoleLevelUpEvent roleLevelUpEvent) {
        Role role = roleLevelUpEvent.getRole();
        SpringUtils.getBean(RoleFlyTaskHandler.class).handleRoleLevelUp(role, roleLevelUpEvent.getLevel());

        SpringUtils.getBean(CampGuideTaskHandler.class).handleRoleLevelUp(role, roleLevelUpEvent.getLevel());

        SpringUtils.getBean(FlyXianMoTaskHandler.class).handleRoleLevelUp(role, roleLevelUpEvent.getLevel());
    }
}
